package com.tianyang.yukuaifuzs.business.main.view;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.View;

import com.tencent.bugly.beta.Beta;
import com.tianyang.yukuaifuzs.BuildConfig;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseFragmentActivity;
import com.tianyang.yukuaifuzs.business.main.adapter.MainAdapter;
import com.tianyang.yukuaifuzs.business.main.bean.AppVersionOutput;
import com.tianyang.yukuaifuzs.business.main.contract.MainConfig;
import com.tianyang.yukuaifuzs.business.main.contract.MainContract;
import com.tianyang.yukuaifuzs.business.main.presenter.MainPresenter;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.common.view.NoScrollViewPager;
import com.tianyang.yukuaifuzs.jump.eventbus.OfflineEvent;
import com.tianyang.yukuaifuzs.jump.eventbus.RefreshUserEvent;
import com.tianyang.yukuaifuzs.module.dialog.CheckVersionDialog;
import com.tianyang.yukuaifuzs.module.dialog.OfflineDialog;
import com.tianyang.yukuaifuzs.module.immersionbar.ImmersionBar;
import com.tianyang.yukuaifuzs.module.tablelayout.NavigationController;
import com.tianyang.yukuaifuzs.module.tablelayout.PageBottomTabLayout;
import com.tianyang.yukuaifuzs.module.tablelayout.item.BaseTabItem;
import com.tianyang.yukuaifuzs.module.tablelayout.item.SpecialTab;
import com.tianyang.yukuaifuzs.utils.AppContextUtil;
import com.tianyang.yukuaifuzs.utils.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * Description：主界面
 */
public class MainActivity extends BaseFragmentActivity implements MainContract.View {

    @BindView(R.id.view_page)
    NoScrollViewPager vViewPager;
    @BindView(R.id.tab_bottom_layout)
    PageBottomTabLayout vTabBottomLayout;

    private MainAdapter mAdapter;
    private CheckVersionDialog checkVersionDialog;

    private long mLastClickBack;

    private MainContract.Presenter mPresenter;
    private OfflineDialog mOfflineDialog;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .statusBarDarkFont(true, 0.5f)
                .init();
    }

    /**
     * 构建tab
     */
    private BaseTabItem newItem(int drawable, int checkedDrawable, String text) {
        SpecialTab mainTab = new SpecialTab(this);
        mainTab.initialize(drawable, checkedDrawable, text);
        return mainTab;
    }

    @Override
    protected void initView() {
        super.initView();
        showMainView();
        mPresenter = new MainPresenter(this);
        if (!BuildConfig.DEBUG)
            Beta.checkUpgrade(false, false);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null && intent.hasExtra(MainConfig.INTENT_IS_CHANGE)) {
            showMainView();
        }
    }

    private void showMainView() {
        String identity = CacheManage.getInstance().getCache(CacheModel.IDENTITY, MainConfig.IDENTITY_MERCHANT);
        mAdapter = new MainAdapter(getSupportFragmentManager(), identity);
        vTabBottomLayout.setVisibility(View.GONE);
        vViewPager.setOffscreenPageLimit(1);
        vViewPager.setNoScroll(false);
        NavigationController navigationController = null;
//        switch (identity) {
//            case MainConfig.IDENTITY_MERCHANT:
//                vTabBottomLayout.setVisibility(View.GONE);
//                break;
//            case MainConfig.IDENTITY_AGENT:
                vTabBottomLayout.setVisibility(View.VISIBLE);
                navigationController = vTabBottomLayout.custom()
                        .addItem(newItem(R.drawable.ic_tab_homepage, R.drawable.ic_tab_homepage_pressed, "首页"))
                        .addItem(newItem(R.drawable.ic_tab_service, R.drawable.ic_tab_service_pressed, "服务商"))
                        .build();
//                break;
//            case MainConfig.IDENTITY_TOP:
//                vTabBottomLayout.setVisibility(View.VISIBLE);
//                navigationController = vTabBottomLayout.custom()
//                        .addItem(newItem(R.drawable.ic_tab_homepage, R.drawable.ic_tab_homepage_pressed, "首页"))
//                        .addItem(newItem(R.drawable.ic_tab_service, R.drawable.ic_tab_service_pressed, "服务商"))
//                        .addItem(newItem(R.drawable.ic_tab_my, R.drawable.ic_tab_my_pressed, "我的"))
//                        .build();
//                break;
//        }
        mAdapter.clear(vViewPager);
        vViewPager.setAdapter(mAdapter);

        //自动适配ViewPager页面切换
        if (navigationController != null)
            navigationController.setupWithViewPager(vViewPager);
        vViewPager.setCurrentItem(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.onRequestUserCenter();
//        OkgoUtils.getOutNetIP(this);
    }

    public void setViewPagerScroll(boolean isScroll) {
        vViewPager.setNoScroll(isScroll);
    }

    @Override
    public void showCheckVersionDialog(AppVersionOutput appVersionOutput) {
        if (appVersionOutput != null) {
            if (checkVersionDialog == null) {
                checkVersionDialog = new CheckVersionDialog(mContext, appVersionOutput);
            }
            checkVersionDialog.show();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOfflineEvent(OfflineEvent event) {
        if (event != null) {
            if (mOfflineDialog == null) {
                mOfflineDialog = new OfflineDialog(this);
            }
            mOfflineDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    AppContextUtil.exit(mContext);
                }
            });
            if (!mOfflineDialog.isShowing()) {
                mOfflineDialog.show();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshUserEvent(RefreshUserEvent event) {
        if (event != null) {
            mPresenter.onRequestUserCenter();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (SystemClock.elapsedRealtime() - mLastClickBack < 2000) {
                mLastClickBack = 0;
                finish();
            } else {
                ToastUtil.show("再按一次退出");
                mLastClickBack = SystemClock.elapsedRealtime();
            }
        }
        return false;
    }

    @Override
    public void setPresenter(MainContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
        if (checkVersionDialog != null && checkVersionDialog.isShowing()) {
            checkVersionDialog.dismiss();
            checkVersionDialog = null;
        }
        if (mOfflineDialog != null && mOfflineDialog.isShowing()) {
            mOfflineDialog.dismiss();
            mOfflineDialog = null;
        }
    }
}
